const std = @import("std");

pub fn build(b: *std.build.Builder) void {
    const flags = [_][]const u8{
        "-Wall",
        "-Wall",
        "-Wextra",
        "-W",
        "-Wstrict-prototypes",
        "-Wwrite-strings",
        "-Wno-missing-field-initializers",
        "-Werror=return-type",
        "-fno-sanitize=undefined"
    };
    const cflags = flags ++ [_][]const u8{
        "-std=c2x",
    };

    const cxxflags = cflags ++ [_][]const u8{
        "-std=c++17", "-fno-exceptions",
    };
    const target = b.standardTargetOptions(.{});
    const mode = b.standardReleaseOptions();

    const pp = b.addStaticLibrary("picohttpparser",null);
    pp.setTarget(target);
    pp.setBuildMode(mode);
    pp.linkLibC();
    pp.force_pic = true;
    pp.addCSourceFiles(&.{
        "deps/picohttpparser/picohttpparser.c"
    }, &cflags);

    const pe = b.addStaticLibrary("picoev",null);
    pe.setTarget(target);
    pe.setBuildMode(mode);
    pe.linkLibC();
    pe.force_pic = true;
    pe.addCSourceFiles(&.{
        "deps/picoev/picoev.c"
    }, &cflags);


    const picoz = b.addExecutable("picoz","src/picoz.zig");
    picoz.setTarget(target);
    picoz.setBuildMode(mode);
    picoz.install();
    picoz.linkLibC();
    picoz.linkLibrary(pp);
    picoz.linkLibrary(pe);
    picoz.addIncludePath("deps/picoev");
    picoz.addIncludePath("deps/picohttpparser");
    

    const run_cmd = picoz.run();
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    const exe_tests = b.addTest("src/picoz.zig");
    exe_tests.setTarget(target);
    exe_tests.setBuildMode(mode);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe_tests.step);
}
